﻿using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;


public class CandidatesItem : MonoBehaviour
{
    public long Id;

    public Image icon;

    public GameObject lockImg;
    public GameObject applyBtn;
    public Text priceText;

    public Text nameText;
    public Text location;
    public Text ageText;
    public Text jobText;
    public Text heightText;
    public Text scoreText;

    public Text requireHeightText;
    public Text requireIncomeText;
    public Text requireEduText;
    
    Action<CandidatesItem> m_OnClick;
    Action<CandidatesItem> m_OnConfirm;
    bool m_Unlock;
    public GameObject selectSign;

    public Text msgText;

    public bool unlock => m_Unlock;
    
    public Color normalColor = Color.blue;
    public Color sucColor = Color.green;
    public Color failColor = Color.red;
    
    public Color notMeetColor = Color.yellow;

    internal void Init(CandidateVCData skin, Action<CandidatesItem> onSelect, Action<CandidatesItem> onConfirm)
    {
        this.Id = skin.candidateId; 
        m_OnClick = onSelect;
        m_OnConfirm = onConfirm;
        m_Unlock = skin.unlock;

        if (priceText)
        {
            priceText.text = skin.price.ToString();

            if(skin.price <= 0)
            {
                priceText.transform.parent.gameObject.SetActive(false);
            }
        }

        ageText.text = skin.fiveDimensions.birthYear.ToString();
        jobText.text = skin.job;
        location.text = skin.location;
        nameText.text = skin.name;
        heightText.text = DataString.GetHeightString(skin.fiveDimensions.Height);
        scoreText.text = $"Score: <color=blue>{UIPropertyShow.GetCandidateScore(skin.fiveDimensions)}</color>";

        var myFiveD = MGame.instance.MUser.userInfo;
        if (myFiveD.height >= skin.require.Height)
            requireHeightText.color = Color.black;
        else
            requireHeightText.color = notMeetColor;
        
        if (myFiveD.income >= skin.require.Income)
            requireIncomeText.color = Color.black;
        else
            requireIncomeText.color = notMeetColor;
        
        if (myFiveD.eduation >= skin.fiveDimensions.Education)
            requireEduText.color = Color.black;
        else
            requireEduText.color = notMeetColor;
        
        requireHeightText.text = DataString.GetHeightString(skin.require.Height);
        requireIncomeText.text = "$" + skin.require.Income;
        requireEduText.text = DataString.GetEducationString(skin.require.Education);
        
        if (lockImg)
            lockImg.SetActive(!skin.unlock);

        var iconSpr = GameResourceManager.LoadCandidateIcon(skin.candidateId);
        icon.sprite = iconSpr;

        RefreshMsg(skin.result);
        
        StartCoroutine(AdjustItemsLayout());
    }

    public void RefreshMsg(int result,string refuseCause = "")
    {
        if (result <= 0)
        {
            msgText.gameObject.SetActive(false);
            applyBtn.SetActive(true);
        }
        else
        {
            if (result == Apply.ApplyResult_Suc)
            {
                applyBtn.SetActive(false);
                ShowMsg("Successfully apply!", sucColor);
            }
            else  if (result == Apply.ApplyResult_Fail)
            {
                applyBtn.SetActive(true);
                if(string.IsNullOrEmpty(refuseCause))
                    ShowMsg($"It was refused", failColor);
                else 
                    ShowMsg($"It was refused \nreason:{refuseCause}", failColor);
            }
            else
            {
                //applyBtn.SetActive(true);
                applyBtn.SetActive(false);
                ShowMsg("Waiting for response...", normalColor);
            }
        }

    }
    IEnumerator AdjustItemsLayout()
    {
        int index = 0;
        float timer = 0;
        while (timer <= 1)
        {
            float step = 0.1f;
            timer += step;
            yield return new WaitForSeconds(step);
            index++;
            foreach (var hlg in GetComponentsInChildren<HorizontalLayoutGroup>())
            {
                hlg.spacing += 0.001f;
            } 
            foreach (var vlg in GetComponentsInChildren<VerticalLayoutGroup>())
            {
                vlg.spacing += 0.001f;
            }
        } 
    }
    
    public void OnClick()
    {
        m_OnClick?.Invoke(this);
    }

    public void OnClickConfirm()
    {
        m_OnConfirm?.Invoke(this);
    }

    void ShowMsg(string msg,Color color)
    {
        msgText.color = color;
        msgText.gameObject.SetActive(true);
        msgText.text = msg;
    }

    internal void UpdateCandidate(CandidateVCData skin)
    {
        if (lockImg)
            lockImg.SetActive(!skin.unlock);

        priceText.transform.parent.gameObject.SetActive(false);
    }

}
